package com.perfect.controller;

import com.perfect.base.BaseController;
import com.perfect.base.ResultInfo;
import com.perfect.model.DiscountReport;
import com.perfect.query.DiscountQuery;
import com.perfect.query.DiscountUseQuery;
import com.perfect.service.DiscountService;
import com.perfect.vo.DiscountDO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 优惠券管理
 */

@Controller
@RequestMapping("discount")
public class DiscountController extends BaseController {


    @Resource
    private DiscountService discountService;

    /**
     * 进入优惠券管理页面
     *
     * @return
     */

    @RequestMapping("index")
    public String index() {
        return "discount/discount_index";
    }



    /**
     * 多条件查询优惠券
     *
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> queryDiscount(DiscountQuery discountQuery) {
        return discountService.listDiscountByParams(discountQuery);
    }



    /**
     * 查询所有优惠券类型
     *
     * @return
     */
    @RequestMapping("queryDiscountType")
    @ResponseBody
    public List<Map<String, Object>> queryDiscountType() {
        return discountService.listDiscountType();
    }


    /**
     * 进入优惠券详情页面
     *
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("discount_data")
    public String discountDataIndex(Integer id, HttpServletRequest request) {
        request.setAttribute("discountData", discountService.listDiscountDataById(id));
        return "discount/discount_data";
    }

    /**
     * 优惠券使用详情 和 多条件查询
     *
     * @param discountUseQuery
     * @return
     */
    @RequestMapping("discountUse")
    @ResponseBody
    public Map<String, Object> queryDiscountUse(Integer discountId, DiscountUseQuery discountUseQuery) {
        return discountService.listDiscountUseByParams(discountUseQuery);
    }


    /**
     * 查询优惠券使用状态
     *
     * @return
     */
    @RequestMapping("queryDiscountUseStatus")
    @ResponseBody
    public List<Map<String, Object>> queryDiscountUseStatus() {
        return discountService.listDiscountUseStatus();
    }

    /**
     * 进入优惠券添加页面
     *
     * @return
     */
    @RequestMapping("add_index")
    public String addIndex() {
        return "discount/discount_add";
    }


    /**
     * 优惠券添加
     * @param discountDO
     * @return
     */

    @RequestMapping("discountAdd")
    @ResponseBody
    public ResultInfo discountAdd(DiscountDO discountDO) {
        discountService.saveDiscount(discountDO);
        return success("添加用户成功");
    }

    /**
     * 进入优惠券修改页面
     * @return
     */
    @RequestMapping("discount_update")
    public String discountUpdateIndex(Integer id,HttpServletRequest request){
        request.setAttribute("discount",discountService.listDiscountById(id));
        return "discount/discount_update";
    }

    /**
     * 编辑优惠券获取当前优惠券的数据
     * @param id
     * @return
     */
    @RequestMapping("discount_update_data")
    @ResponseBody
    public DiscountDO selectDiscount(Integer id){
        return discountService.listDiscountById(id);
    }

    /**
     * 优惠券修改
     * @param discountDO
     * @return
     */
    @RequestMapping("discountUpdate")
    @ResponseBody
    public ResultInfo discountUpdate(DiscountDO discountDO) {
        discountService.updateDiscount(discountDO);
        return success("添加用户成功");
    }


    /**
     * 删除优惠券
     * @param id
     * @return
     */
    @RequestMapping("deleteDiscount")
    @ResponseBody
    public ResultInfo deleteDiscount(Integer id){
        discountService.removeDiscount(id);
        return success("优惠券删除成功");
    }


    /**
     * 查询不同种类的优惠券总数
     * @return
     */
    @RequestMapping("discountCount")
    @ResponseBody
    public Map<String,Object> discountCount(){
        return discountService.listDiscountCount();
    }
}
